﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Drawing.Drawing2D;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private DateTime lastwrite=DateTime.Now;

        private void button1_Click(object sender, EventArgs e)
        {
            if (tbDossierPath.Text != "")
            {
                openFileDialog1.InitialDirectory = Path.GetDirectoryName(tbDossierPath.Text);
                openFileDialog1.FileName = Path.GetFileName(tbDossierPath.Text);
            }
            else
            {
                openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString() + @"\wargaming.net\WorldOfTanks\dossier_cache\";
            }
            openFileDialog1.Filter = "Dossier cache files(*.dat)|*.dat";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbDossierPath.Text = openFileDialog1.FileName;
                Properties.Settings.Default.DossierPath = openFileDialog1.FileName;
                Properties.Settings.Default.Save();
            }
        }

        private void tbDossierPath_TextChanged(object sender, EventArgs e)
        {
        }

        private void bChooseExecutor_Click(object sender, EventArgs e)
        {
            if (tbExecute.Text != "")
            {
                openFileDialog1.InitialDirectory = Path.GetDirectoryName(tbExecute.Text);
                openFileDialog1.FileName = Path.GetFileName(tbExecute.Text);
            }
            else
            {
                openFileDialog1.InitialDirectory = Environment.CurrentDirectory;
            }
            openFileDialog1.Filter = "";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbExecute.Text = openFileDialog1.FileName;
                Properties.Settings.Default.ExecutorPath = openFileDialog1.FileName;
                Properties.Settings.Default.Save();
            }
        }

        private void bMonitor_Click(object sender, EventArgs e)
        {
            if (fileSystemWatcher1.EnableRaisingEvents)
            {
                fileSystemWatcher1.EnableRaisingEvents = false;
                bMonitor.Text = "Start monitoring";
                lbLog.Items.Add("Stop monitoring @ " + DateTime.Now.ToString());
            }
            else
            {
                fileSystemWatcher1.Path = Path.GetDirectoryName(tbDossierPath.Text);
                fileSystemWatcher1.Filter = Path.GetFileName(tbDossierPath.Text);
                fileSystemWatcher1.EnableRaisingEvents = true;
                bMonitor.Text = "Stop monitoring";
                lbLog.Items.Add("Monitoring since " + DateTime.Now.ToString());
            }

            Properties.Settings.Default.ExecutorArguments = tbArguments.Text;
            Properties.Settings.Default.Save();
        }

        private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
        {
            try
            {
                FileInfo fi = new FileInfo(tbDossierPath.Text);
                if (fi.LastWriteTime > lastwrite)//workaround for multiple firing
                {
                    fileSystemWatcher1.EnableRaisingEvents = false; 

                    var s = Path.GetFileName(tbDossierPath.Text) + " changed on " + DateTime.Now.ToString();
                    lbLog.Items.Add(s);
                    ProcessStartInfo startInfo = new ProcessStartInfo(tbExecute.Text);
                    startInfo.WorkingDirectory = Path.GetDirectoryName(tbExecute.Text);
                    startInfo.Arguments = tbArguments.Text;
                    startInfo.CreateNoWindow = true;
                    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    Process.Start(startInfo);
                    lastwrite = fi.LastWriteTime;
                }
            }
            finally
            {
                fileSystemWatcher1.EnableRaisingEvents = true;
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            tbDossierPath.Text = Properties.Settings.Default.DossierPath;
            tbExecute.Text = Properties.Settings.Default.ExecutorPath;
            tbArguments.Text = Properties.Settings.Default.ExecutorArguments;
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                UpdateNotifyIcon("monitoring");
                notifyIcon1.Visible = true;
                notifyIcon1.ShowBalloonTip(500);
                this.Hide();
                this.ShowInTaskbar = false;
            }
            else if (FormWindowState.Normal == this.WindowState)
            {
                notifyIcon1.Visible = false;
                this.ShowInTaskbar = true;
            }
        }

        private void UpdateNotifyIcon(string pMode)
        {
            Icon oIcon = null;
            int dimension = 16;
            try
            {
                var bm = new Bitmap(dimension, dimension);
                var g = Graphics.FromImage((Image)bm);
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.FillRectangle(Brushes.Transparent, new Rectangle(0, 0, bm.Width, bm.Height));

                g.FillPie(Brushes.Linen, new Rectangle(0, 0, bm.Width, bm.Height), -90, 360);

                oIcon = Icon.FromHandle(bm.GetHicon());
                g.Dispose();
                bm.Dispose();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }

            notifyIcon1.Icon = oIcon;

        }

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            this.ShowInTaskbar = !this.ShowInTaskbar;
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.ShowInTaskbar = true;
            this.Show();
            this.WindowState = FormWindowState.Normal;
            notifyIcon1.Visible = false;
        }

        private void bLaunchWoT_Click(object sender, EventArgs e)
        {
            if (System.Diagnostics.Process.GetProcessesByName("wotlauncher").Length > 0 || System.Diagnostics.Process.GetProcessesByName("worldoftanks").Length > 0) return;
            if (Properties.Settings.Default.WoTPath != "")
            {
                openFileDialog1.InitialDirectory = Path.GetDirectoryName(Properties.Settings.Default.WoTPath);
                openFileDialog1.FileName = Path.GetFileName(Properties.Settings.Default.WoTPath);
            }
            openFileDialog1.Filter = "World of Tanks launcher (wotlauncher.exe)|wotlauncher.exe";

            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Properties.Settings.Default.WoTPath = openFileDialog1.FileName;
                ProcessStartInfo startInfo = new ProcessStartInfo(Properties.Settings.Default.WoTPath);
                startInfo.WorkingDirectory = Path.GetDirectoryName(Properties.Settings.Default.WoTPath);
                Process.Start(startInfo);
            }
        }
    }
}
